package com.ddwl.goods.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.dao.ItemTopicDao;
import com.ddwl.goods.dao.TopicDao;
import com.ddwl.goods.dao.model.Topic;
import com.ddwl.goods.model.ItemTopic;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.ItemTopicBo;
import com.ddwl.schema.bo.goods.ItemTopicSortBo;
import com.ddwl.schema.bo.goods.TopicBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.TopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

@Service
public class TopicService {

    @Autowired
    private TopicDao topicDao;

    @Autowired
    private ItemTopicDao itemTopicDao;


    /**
     * 创建或更新
     * @param topicBo
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    public TopicVo create(TopicBo topicBo) {
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicBo, topic);
        if (topicBo.getId() == null) {
            topicDao.insert(topic);
            return BeanConverUtils.convertBean(topic,TopicVo.class);
        } else {
            topicDao.updateByPrimaryKeySelective(topic);
            return BeanConverUtils.convertBean(topicDao.selectByPrimaryKey(topicBo.getId()),TopicVo.class);
        }
    }


    /**
     * 查询列表
     * @param pageBo
     * @return
     */
    public PageVo<TopicVo> list(PageBo<TopicBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(),pageBo.isAsc()));
        Topic topic = new Topic();
        BeanUtils.copyProperties(pageBo.getParam(), topic);
        PageInfo<Topic> pageInfo = new PageInfo<>(topicDao.select(topic));
        List<TopicVo> topics = BeanConverUtils.convertBeanList(pageInfo.getList(), TopicVo.class);
        ItemTopic itemTopic=new ItemTopic();
        for(TopicVo topicVo:topics){
            itemTopic.setTopicId(topicVo.getId());
            List<ItemTopic> list= itemTopicDao.select(itemTopic);
            List<String> ids=new ArrayList<>();
            for(ItemTopic itemTopic1:list){
                ids.add(itemTopic1.getItemId());
            }
            topicVo.setIds(ids);
        }
        return new PageVo<>(pageInfo.getTotal(), pageBo.getPage(), pageBo.getPageSize(), topics);
    }

    /**
     * 批量绑定商品列表
     * @param topicBo
     * @return
     */
    public int addItem(ItemTopicBo topicBo){
        itemTopicDao.deleteByTopicId(topicBo.getTopicId());
        List<ItemTopic> list=new ArrayList<>();
        for(ItemTopicBo.ItemSort itemSort:topicBo.getItemList()){
            ItemTopic topic=new ItemTopic();
            topic.setId(UUIDGenerator.getShortUUID());
            topic.setItemId(itemSort.getItemId());
            topic.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
            topic.setGmtModified(Instant.now());
            topic.setGmtCreated(Instant.now());
            topic.setItemSort(itemSort.getItemSort());
            topic.setTopicId(topicBo.getTopicId());
            list.add(topic);
        }
        return  itemTopicDao.insertByList(list);
    }


    /**
     * 专题商品排序
     * @param itemTopicSortBo
     */
    public void topicItemSort(ItemTopicSortBo itemTopicSortBo){
        itemTopicDao.deleteByItemTopicId(itemTopicSortBo.getItemId(),itemTopicSortBo.getTopicId());
        ItemTopic itemTopic=new ItemTopic();
        itemTopic.setTopicId(itemTopicSortBo.getTopicId());
        itemTopic.setItemId(itemTopicSortBo.getItemId());
        itemTopic.setItemSort(itemTopicSortBo.getSort());
        itemTopicDao.insert(itemTopic);
    }

    /**
     * 删除专题及相关
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Throwable.class)
    public int delete(String id){
        Topic topic=new Topic();
        topic.setId(id);
        topic.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        itemTopicDao.deleteByTopicId(id);
       return topicDao.updateByPrimaryKeySelective(topic);
    }
}
